package com.oldking.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CommonTimeUtil {

    public static String secondToTime(long second) {
        long days = second / 86400;            //转换天数
        second = second % 86400;            //剩余秒数
        long hours = second / 3600;            //转换小时
        second = second % 3600;                //剩余秒数
        long minutes = second / 60;            //转换分钟
        second = second % 60;                //剩余秒数
        if (days > 0) {
            return timeFormat(hours) + ":" + timeFormat(minutes) + ":" + timeFormat(second);
        } else {
            return timeFormat(hours) + ":" + timeFormat(minutes) + ":" + timeFormat(second);
        }
    }

    public static int Stringtime2Second(String dateTime) {
        int res = 1;
        String[] split = dateTime.split(":");
        if (split.length == 3) {
            res = Integer.valueOf(split[0].trim()) * 60 * 60 + Integer.valueOf(split[1].trim()) * 60 + Integer.valueOf(split[2].trim());
        }
        if (split.length == 2) {
            res = Integer.valueOf(split[0].trim()) * 60 + Integer.valueOf(split[1].trim());
        }
        return res;
    }

    public static String timeFormat(long t) {
        if (t < 10) {
            return "0" + t;
        }
        return String.valueOf(t);
    }


}
